package com.yangxiangrice.config;

import com.yangxiangrice.config.realm.MyRealm;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import sun.security.action.PutAllAction;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Description: TODO
 * @author: shixiaojian
 * @date: 2021年11月23日 20:38
 */
@Configuration
public class ShiroConfig {
    @Bean(value = "shiroFilterFactoryBean")
    ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        filterChainDefinitionMap.put("/menu/list", "anon");
        filterChainDefinitionMap.put("/userlist/login", "anon");
        filterChainDefinitionMap.put("/**", "authc");
      /*  *//*登陆页面放行*//*
        filterChainDefinitionMap.put("/toIndex", "anon");
        filterChainDefinitionMap.put("/user/login","anon");
        *//*放行test*//*
        filterChainDefinitionMap.put("/toTest","anon");
        filterChainDefinitionMap.put("/tea/fileSelect","anon");
        *//*放行静态资源*//*
        filterChainDefinitionMap.put("/css/**", "anon");
        filterChainDefinitionMap.put("/fonts/**", "anon");
        filterChainDefinitionMap.put("/images/**", "anon");
        filterChainDefinitionMap.put("/js/**", "anon");
        filterChainDefinitionMap.put("/lib/**", "anon");
        filterChainDefinitionMap.put("/pdf/**", "anon");


        shiroFilterFactoryBean.setLoginUrl("/toIndex");
        shiroFilterFactoryBean.setSuccessUrl("/main");
        shiroFilterFactoryBean.setUnauthorizedUrl("/error");*/
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }


    @Bean(value = ("securityManager"))
    DefaultWebSecurityManager getDefaultWebSecurityManager(Realm myRealm) {
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(myRealm);
        return defaultWebSecurityManager;
    }

    @Bean(value = "myRealm")
    Realm getRealm() {
        MyRealm myRealm = new MyRealm();
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        hashedCredentialsMatcher.setHashAlgorithmName("MD5");
        hashedCredentialsMatcher.setHashIterations(1024);
        myRealm.setCredentialsMatcher(hashedCredentialsMatcher);
        return myRealm;
    }
}
